<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const DivByK = (A, K) => {
      let preSumModK = 0;
      let count = 0;
      const map = { 0: 1 };
      for (let i = 0; i < A.length; i++) {
        preSumModK = (preSumModK + A[i]) % K; // 递推式子
        if (preSumModK < 0) {
          preSumModK += K;
        }
        if (map[preSumModK]) {      // 已经存在于map
          count += map[preSumModK]; // 把对应的次数累加给count
          map[preSumModK]++;        // 并且更新出现次数，次数+1
        } else {
          map[preSumModK] = 1;      // 之前没出现过，初始化值为1
        }
      }
      return count;
    };
    console.log(DivByK(nums = [4, 5, 0, -2, -3, 1], k = 5))
  </script>
</body>

</html>